﻿using System;
using System.Collections;
using System.Runtime.CompilerServices;
using UnityEngine;

public class AdManager : MonoBehaviour
{
    public static bool TestMode = false;

    public static string Owner = "著作权人：北京光耀大地科技有限公司"; //健康忠告-著作权人
    public static string License = "登记号：2022SR0321687"; //健康忠告v-登记号
    public static int AgeLimit = 12; //健康忠告-适龄 8 12 16

    public static string Company = "厦门泰酷文化科技有限公司"; //隐私-公司
    public static string Email = "客服邮箱：2572555395@qq.com"; //隐私-联系方式

    public static int Year = 2024;
    public static int Month = 5;
    public static int Day = 15;
    public static int Hour = 18;

    private static string AppId = "105712532";
    private static string VideoId = "f701564b61a94de6902e6829432646b6";
    private static string CustomId = "ffd64f897db64e31b7834587382c9405";
    private static string BannerId = "9639c90843e5474db4557b9ed948641d";


    static AdManager _single;
    public static AdManager single
    {
        get
        {
            if (_single == null)
            {
                GameObject gameObject = new GameObject("AdManager");
                _single = gameObject.AddComponent<AdManager>();
                DontDestroyOnLoad(gameObject);
            }
            return _single;
        }

    }

    public void ShowBanner()
    {
        if (TestMode) return;
#if UNITY_EDITOR

#elif UNITY_ANDROID
#else
        ShowQGBanner();
#endif
    }

    public void ShowNative(float delay = 1.0f)
    {
        Invoke("Native", delay);
    }
    void Native()
    {
        Debug.Log("弹出原生广告");
        if (TestMode) return;
#if UNITY_EDITOR

#elif UNITY_ANDROID
#else
        ShowQGNative();
#endif
        if (!AdManager1.instance.ShieldAddress) ShowBanner();
    }

    public void ShowVideo(Action action)
    {
        Debug.Log("弹出视频广告");
        if (TestMode)
        {
            action?.Invoke();
            return;
        }
#if UNITY_EDITOR
        action?.Invoke();
        return;
#elif UNITY_ANDROID
#else
        ShowQGVideo(action);
#endif
    }

    public static void Quit()
    {
#if UNITY_EDITOR
        //GameManager.Instance.Quit();
#elif UNITY_ANDROID
#else
        QGMiniGameManager.Instance.ExitApplication();
#endif
    }

    #region Android

    private static AndroidJavaClass jc;
    private static AndroidJavaObject jo;

    private static void CheckNull()
    {
        if (jc == null || jo == null)
        {
            jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
            jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        }
    }

    private static void ShowAndroidBanner()
    {
        CheckNull();
        jo.Call("ShowBanner");
    }

    private static void ShowAndroidNative()
    {
        CheckNull();
        jo.Call("ChaPing");
    }

    private static void ShowAndroidVideo(Action action)
    {
        jo.Call("GameVideo1", action);
    }

    private static void ShowAndroidKeFu() => jo.Call("KeFu");

    private static void ShowAndroidMoreGame() => jo.Call("MoreGame");

    private static void ShowAndroidPrivacy() => jo.Call("Hide");

    private static void AndroidQuitGame() => jo.Call("Quit");

    #endregion
    /* 
        #region VIVO

        /// <summary>Banner 广告</summary>
        private static void ShowQGBanner()
        {
            if (InTimeRange(Year, Month, Day, Hour)) return;

            var bannerAd = QG.CreateBannerAd(new QGCreateBannerAdParam
            { adIntervals = 30, posId = BannerId });

            bannerAd.OnLoad(() =>
            {
                bannerAd.Show(
                    (msg) => { Debug.Log("QG.bannerAd.Show success = " + JsonUtility.ToJson(msg)); },
                    (msg) => { Debug.Log("QG.bannerAd.Show fail = " + msg.errMsg); }
                );
            });
            bannerAd.OnError((QGBaseResponse msg) =>
            {
                Debug.Log("QG.bannerAd.OnError success = " + JsonUtility.ToJson(msg));
            });
        }

        /// <summary>原生广告</summary>
        public void ShowQGNative()
        {
            if (InTimeRange(Year, Month, Day, Hour)) return;
            var customAd = QG.CreateCustomAd(new QGCreateCustomAdParam()
            {
                posId = CustomId,
                style = new Style { left = Screen.width / 2 - 200, top = Screen.height / 2 }
            });
            customAd.OnLoad(() =>
            {
                Debug.Log("QG.customAd.OnLoad success = ");
                customAd.Show(
                    (msg) => { Debug.Log("QG.customAd.Show success = " + JsonUtility.ToJson(msg)); },
                    (msg) => { Debug.Log("QG.customAd.Show fail = " + msg.errMsg); }
                );
            });
            customAd.OnError((QGBaseResponse msg) =>
            {
                Debug.Log("QG.customAd.OnError success = " + JsonUtility.ToJson(msg));
            });
            customAd.OnHide(() => { Debug.Log("QG.customAd.OnHide success "); });
        }



        /// <summary>视频广告</summary>
        public void ShowQGVideo(Action action)
        {
            var rewardedVideoAd = QG.CreateRewardedVideoAd(new QGCommonAdParam { posId = VideoId });

            rewardedVideoAd.OnLoad(() =>
            {
                rewardedVideoAd.Show(
                    (msg) => { Debug.Log("QG.rewardedVideoAd.Show success = " + JsonUtility.ToJson(msg)); },
                    (msg) => { Debug.Log("QG.rewardedVideoAd.Show fail = " + msg.errMsg); }
                );
            });
            rewardedVideoAd.OnError((QGBaseResponse msg) =>
            {
                Tip.Show("视频广告加载失败");
                Debug.Log("QG.rewardedVideoAd.OnError success = " + JsonUtility.ToJson(msg));
            });
            rewardedVideoAd.OnClose((QGRewardedVideoResponse msg) =>
            {
                if (msg.isEnded)
                {
                    action?.Invoke();
                    Debug.Log("QG.rewardedVideoAd.OnClose success = " + " 播放成功");
                }
                if (!AdManager1.instance.ShieldAddress) ShowNative();
            });
        }

        public static bool InTimeRange(int year, int month, int day, int hour, int minute = 0, int second = 0) => DateTime.Now < new DateTime(year, month, day, hour, minute, second);


        #endregion
     */
    //#region OPPO

    ///// <summary>Banner 广告</summary>
    //private static void ShowQGBanner()
    //{
    //    if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;

    //    var bannerAd = QG.CreateBannerAd(new QGCreateBannerAdParam()
    //    {
    //        adUnitId = BannerId
    //    });
    //    Debug.Log("创建Banner广告开始运行");
    //    bannerAd.OnLoad(() => { Debug.Log("banner广告加载成功"); });
    //    bannerAd.OnError((QGBaseResponse msg) => { Debug.Log("QG.bannerAd.OnError success = " + JsonUtility.ToJson(msg)); });
    //}

    ///// <summary>原生广告</summary>
    //public void ShowQGNative()
    //{
    //    if (ZTool.InTimeRange(Year, Month, Day, Hour)) return;
    //    Invoke("Native", 1);
    //}

    //void Native()
    //{
    //    var customAd = QG.CreateCustomAd(new QGCreateCustomAdParam()
    //    {
    //        adUnitId = CustomId //上文下图
    //    });
    //    Debug.Log("创建原生模板广告开始运行");
    //    customAd.OnLoad(() => { Debug.Log("原生模板广告加载成功"); });
    //    customAd.Show(
    //        (msg) => { Debug.Log("原生模板广告展示成功 = " + JsonUtility.ToJson(msg)); },
    //        (msg) => { Debug.Log("原生模板广告展示失败 = " + msg.errMsg); }
    //    );
    //    customAd.OnError((QGBaseResponse msg) => { Debug.Log("QG.customAd.OnError success = " + JsonUtility.ToJson(msg)); });
    //    customAd.OnHide(() => { Debug.Log("QG.customAd.OnHide success "); });
    //}



    ///// <summary>视频广告</summary>
    //public void ShowQGVideo(Action action)
    //{
    //    var rewardedVideoAd = QG.CreateRewardedVideoAd(new QGCommonAdParam()
    //    {
    //        adUnitId = VideoId
    //    });
    //    Debug.Log("创建激励视频开始运行");
    //    rewardedVideoAd.OnLoad(() =>
    //    {
    //        Debug.Log("激励视频广告加载成功");
    //        rewardedVideoAd.Show();
    //    });
    //    rewardedVideoAd.OnError((QGBaseResponse msg) => { 
    //        Debug.Log("QG.rewardedVideoAd.OnError success = " + JsonUtility.ToJson(msg));
    //        Tip.Show("视频广告加载失败");
    //    });
    //    rewardedVideoAd.OnClose((QGRewardedVideoResponse msg) =>
    //    {
    //        if (msg.isEnded)
    //        {
    //            action?.Invoke();
    //            Debug.Log("激励视频广告完成，发放奖励");
    //        }
    //        else
    //        {
    //            Debug.Log("激励视频广告取消关闭，不发放奖励");
    //        }
    //        if (!AdManager1.instance.ShieldAddress) ShowNative();
    //    });
    //}

    //#endregion
}